<?php

namespace common\models;

use Yii;

/**
 * This is the model class for table "complaints".
 *
 * @property int             $id
 * @property int             $uid 用户id
 * @property int             $type 类型
 * @property string          $contact_name 联系人姓名
 * @property string          $contact_phone 联系人电话
 * @property string          $content 详细内容
 * @property int             $created_at
 * @property int             $updated_at
 * @property int             $target_id
 * @property int             status
 *
 * @property User            $u
 * @property ComplaintsImg[] $complaintsImgs
 */
class Complaints extends \yii\db\ActiveRecord
{

    const STATUS_WAIT = 0;
    const STATUS_DEAL = 1;

    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'complaints';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['uid', 'type', 'created_at', 'updated_at', 'status', 'target_id'], 'integer'],
            [['contact_name', 'contact_phone', 'content'], 'string', 'max' => 255],
            [
                ['uid'],
                'exist',
                'skipOnError'     => true,
                'targetClass'     => User::className(),
                'targetAttribute' => ['uid' => 'id']
            ],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id'            => 'ID',
            'uid'           => '用户id',
            'type'          => '类型',
            'contact_name'  => '联系人姓名',
            'contact_phone' => '联系人电话',
            'content'       => '详细内容',
            'created_at'    => '举报时间',
            'updated_at'    => 'Updated At',
            'target_id'     => "目标id",
            'status'        => '处理状态'
        ];
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getU()
    {
        return $this->hasOne(User::className(), ['id' => 'uid']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getComplaintsImgs()
    {
        return $this->hasMany(ComplaintsImg::className(), ['complaint_id' => 'id']);
    }

    public function beforeSave($insert)
    {
        if ($insert) {
            $this->created_at = $this->updated_at = time();
        } else {
            $this->updated_at = time();
        }

        return parent::beforeSave($insert);
    }


}
